function create_clinical_vs_lab_ppg_pulse_wave_plot
% CREATE_CLINICAL_VS_LAB_PPG_PULSE_WAVE_PLOT creates a plot of sample
% photoplethysmogram (PPG) pulse waves acquired simultaneously from a clinical 
% monitor and high-fidelity laboratory equipment
%
%               create_clinical_vs_lab_ppg_pulse_wave_plot
%
%	This file creates the image at:
%           https://commons.wikimedia.org/wiki/File:Clinical_vs_laboratory_photoplethysmogram_(PPG)_pulse_waves.svg
%   The image is available under the CC BY 4.0 licence.
%
%   The image was published in:
%           Charlton P.H. and Marozas V.; 'Wearable photoplethysmography 
%           devices'; in Photoplethysmography; Kyriacou P.A., Allen J., Eds.;
%           Elsevier, 2021. https://peterhcharlton.github.io/publication/wearable_ppg_chapter/
%   Please cite this publication when using this image.
%   
%   Output:
%       image files in the same folder as this script
%
%   Version:
%       v.1.0: Created on 26-Aug-2021, using MATLAB R2020a. 
%
%   MIT Licence:
%       Copyright 2021 Peter H. Charlton
%       Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
%       The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
%       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%

% setup universal parameters
up = setup_up;

% load data
data = load_data;

% make plot
make_plot(data);

% save plot
save_plot(up);

end

function up = setup_up

% specify save path as the path of this script
up.savepath = [fileparts(mfilename('fullpath')), filesep];

fprintf(['\n Creating: ''finger vs ear PPG pulse wave'' plot.', ...
    '\n This program comes with ABSOLUTELY NO WARRANTY.', ...
    '\n This is free software, and you are welcome to redistribute it', ...
    '\n under certain conditions: see the Licence in the file header for details.']);

end

function data = load_data

% Data are from the Vortal Dataset, as described in:
%   Charlton, P.H.; Bonnici, T.; Tarassenko, L.; Alastruey, J.; Clifton, D.A.; Beale, R.; Watkinson, P.J. Extraction of respiratory signals from the electrocardiogram and photoplethysmogram: technical and physiological determinants. Physiol. Meas. 2017, 38, 669–690, doi:10.1088/1361-6579/aa670e.

% Laboratory PPG data
data.lab_ppg.v = [0,-0.00154520320225809,-0.00240601906053355,-0.00250961449259765,-0.00178488917874205,-0.000163644025535376,0.00241826160515198,0.00601978706333720,0.0106936524541344,0.0164853685308202,0.0234323842042037,0.0315633634773719,0.0408976163841221,0.0514446959403562,0.0632041670825439,0.0761655553415680,0.0903084725842066,0.105602914147132,0.122009717240092,0.139481168694067,0.157961740980065,0.177388935926067,0.197694213368145,0.218803978154017,0.240640600912238,0.263123445864733,0.286169882676208,0.309696256607216,0.333618797599685,0.357854452565494,0.382321622303396,0.406940797927352,0.431635086950047,0.456330630087612,0.480956907328616,0.505446940819852,0.529737403306879,0.553768642657308,0.577484638803019,0.600832904683726,0.623764348747809,0.646233115774548,0.668196420017019,0.689614386625828,0.710449911672564,0.730668550805198,0.750238445832199,0.769130291731111,0.787317347995397,0.804775487676946,0.821483287912769,0.837422147998096,0.852576431681785,0.866933619978294,0.880484465144779,0.893223132155218,0.905147319747392,0.916258346097617,0.926561192048129,0.936064493800737,0.944780477545766,0.952724833630536,0.959916531012061,0.966377572034895,0.972132691039327,0.977209007409641,0.981635636380277,0.985443274299686,0.988663765433100,0.991329667426462,0.993473823979602,0.995128961861874,0.996327320539133,0.997100328833917,0.997478332970097,0.997490385507402,0.997164098885008,0.996525563940029,0.995599334382151,0.994408472718294,0.992974649555830,0.991318288458374,0.989458749285618,0.987414533652873,0.985203503576295,0.982843098407366,0.980350537543468,0.977742998773269,0.975037756908124,0.972252279400836,0.969404266993168,0.966511638180870,0.963592454204461,0.960664784198984,0.957746515674922,0.954855114848464,0.952007347128301,0.949218964381864,0.946504376323165,0.943876312355275,0.941345494105874,0.938920327931455,0.936606632048341,0.934407411738434,0.932322691833375,0.930349416014208,0.928481420035195,0.926709481192825,0.925021449287927,0.923402452578069,0.921835178249253,0.920300218858261,0.918776478714617,0.917241623383076,0.915672566078735,0.914045970488086,0.912338763046893,0.910528632758669,0.908594510345827,0.906517009068354,0.904278817947193,0.901865036277300,0.899263442045671,0.896464686122814,0.893462412277752,0.890253300663846,0.886837037606161,0.883216213823693,0.879396158386896,0.875384718862677,0.871191995953106,0.866830040692182,0.862312529694835,0.857654429726485,0.852871660116756,0.847980766549182,0.842998615698993,0.837942117835971,0.832827986131919,0.827672535203040,0.822491523404903,0.817300040111842,0.812112435470590,0.806942291363414,0.801802429096807,0.796704946266696,0.791661279657433,0.786682283148678,0.781778315639594,0.776959329101914,0.772234951647569,0.767614557803235,0.763107320964876,0.758722244434543,0.754468164425689,0.750353729750075,0.746387352914502,0.742577135502286,0.738930773564902,0.735455442459287,0.732157670564627,0.729043203760913,0.726116871525069,0.723382455158785,0.720842571710787,0.718498571108560,0.716350459206546,0.714396845092912,0.712634919625214,0.711060464458870,0.709667894753005,0.708450331964390,0.707399704122940,0.706506873735845,0.705761786314971,0.705153634068553,0.704671031507699,0.704302192836325,0.704035110756038,0.703857724778414,0.703758077143296,0.703724449640601,0.703745480924233,0.703810255681447,0.703908370308676,0.704029968950964,0.704165755938000,0.704306979823966,0.704445399971620,0.704573233732319,0.704683090724101,0.704767896454395,0.704820815914175,0.704835177954840,0.704804405873167,0.704721960063041,0.704581295823250,0.704375838312108,0.704098980604230,0.703744099193418,0.703304592434691,0.702773937643041,0.702145767964000,0.701413961164176,0.700572739108269,0.699616776707278,0.698541309158366,0.697342236028818,0.696016219314343,0.694560765572959,0.692974293806358,0.691256180542506,0.689406784689630,0.687427446691799,0.685320461451212,0.683089027161478,0.680737170235281,0.678269648637874,0.675691836932556,0.673009596279007,0.670229134458109,0.667356860335833,0.664399236194050,0.661362634405730,0.658253202548861,0.655076740791831,0.651838594477371,0.648543566675216,0.645195851043447,0.641798987920228,0.638355844686596,0.634868615601805,0.631338847029891,0.627767479914724,0.624154913674769,0.620501078289495,0.616805523979368,0.613067517591419,0.609286140403000,0.605460391362973,0.601589285017448,0.597671945525235,0.593707692157942,0.589696115402778,0.585637139602360,0.581531074446135,0.577378651293140,0.573181047234910,0.568939894544360,0.564657276185740,0.560335709708057,0.555978119518976,0.551587799849292,0.547168367344598,0.542723708041167,0.538257918521838,0.533775240385773,0.529279994540104,0.524776511095804,0.520269059913240,0.515761780245978,0.511258612538355,0.506763232487789,0.502278986283689,0.497808834079367,0.493355294953837,0.488920401382417,0.484505660984504,0.480112025798369,0.475739872697907,0.471388997184932,0.467058617486558,0.462747393439526,0.458453457300874,0.454174462038880,0.449907640920435,0.445649883341318,0.441397820797772,0.437147926275518,0.432896621347688,0.428640392437477,0.424375904881856,0.420100121259068,0.415810413484255,0.411504667702696,0.407181376728270,0.402839716794191,0.398479604162942,0.394101728912591,0.389707565118251,0.385299351932043,0.380880050030782,0.376453268665475,0.372023170000278,0.367594346723024,0.363171683708223,0.358760202400230,0.354364896813903,0.349990566011530,0.345641649782336,0.341322073457521,0.337035109786186,0.332783263087205,0.328568181734729,0.324390603837149,0.320250338723853,0.316146287101092,0.312076500132649,0.308038276725771,0.304028294366430,0.300042772033277,0.296077657851727,0.292128832063282,0.288192321654589,0.284264512243735,0.280342351262956,0.276423530808539,0.272506641790212,0.268591291850682,0.264678178482965,0.260769112728807,0.256866990250643,0.252975705493690,0.249100015243580,0.245245351401196,0.241417587699058,0.237622771380915,0.233866828315249,0.230155252385584,0.226492793782607,0.222883157463108,0.219328729648939,0.215830340438360,0.212387077871407,0.208996166256939,0.205652912086818,0.202350727462947,0.199081233586551,0.195834444888838,0.192599026853731,0.189362626235860,0.186112257027314,0.182834734986736,0.179517141069121,0.176147299522484,0.172714248102133,0.169208683428559,0.165623362451157,0.161953440468586,0.158196727658450,0.154353853848503,0.150428327470065,0.146426484641028,0.142357322356765,0.138232220550986,0.134064559168686,0.129869240639765,0.125662134606958,0.121459463545631,0.117277153042249,0.113130170991292,0.109031884159153,0.104993460919144,0.101023343264706,0.0971268184715369,0.0933057102313434,0.0895582119351396,0.0858788740319322,0.0822587572978728,0.0786857555970993,0.0751450871352356,0.0716199431563290,0.0680922820329298,0.0645437471129189,0.0609566821615322,0.0573152152215805,0.0536063748889483,0.0498212052253054,0.0459558380994513,0.0420124887537017,0.0380003347831542,0.0339362457583826,0.0298453385725087,0.0257613229512060,0.0217266298018956,0.0177923011988388,0.0140176427753105,0.0104696382020421,0.00722213642237806,0.00435482709644939,0.00195203107717241,0.000101333181148932,-0.00110790776762939,-0.00158613266945048,-0.00124519927038022,0];
data.lab_ppg.fs = 500; % in Hz
data.lab_ppg.t = [0:length(data.lab_ppg.v)-1]./data.lab_ppg.fs;

% Clinical Monitor PPG data
data.clin_ppg.v = [0,-0.00304501869744991,0.00972685572609725,0.0440316949178984,0.103171998267101,0.186792944185726,0.290544549278324,0.406815211480127,0.526320537026520,0.640032270400509,0.740840683845615,0.824489679478627,0.889630426784646,0.937164863655092,0.969258705909170,0.988426757646690,0.996954981302550,0.996715635352165,0.989259593061640,0.976000634603094,0.958343072105460,0.937697324783211,0.915413131098078,0.892694224534925,0.870539193392922,0.849710968045615,0.830709147808550,0.813724712690251,0.798587641405117,0.784748246644065,0.771337638179180,0.757324774396433,0.741742044176713,0.723914602640920,0.703621683315534,0.681144021507414,0.657195232026565,0.632773385228977,0.608985006631696,0.586884993983035,0.567353894714852,0.551014489730619,0.538182488991384,0.528850050462109,0.522706426803026,0.519198148640017,0.517620067440843,0.517215349706100,0.517257529293206,0.517096701610084,0.516171084246055,0.514003034057258,0.510203604891539,0.504498163983312,0.496764701686536,0.487060138658723,0.475609677891159,0.462751476052127,0.448853564964106,0.434236666805335,0.419134443452290,0.403702530493191,0.388061420916849,0.372341575333434,0.356701591571393,0.341309454430529,0.326300351907698,0.311738028957734,0.297603062943317,0.283814684474776,0.270274234707626,0.256908980186428,0.243698476897540,0.230677251867850,0.217918740129335,0.205510357107526,0.193528511792104,0.182019564382304,0.170991578144389,0.160421256337639,0.150276783384051,0.140548605156468,0.131270759697270,0.122514003498591,0.114344883133456,0.106768161470320,0.0996886409297039,0.0929251476069395,0.0862786007733982,0.0796126979107937,0.0728806352175595,0.0660521892291454,0.0589645611261598,0.0512041329986618,0.0421684818879180,0.0314122139268080,0.0192440438371498,0.00737109510723499,-0.000728424764254933,0];
data.clin_ppg.fs = 125; % in Hz
data.clin_ppg.t = [0:length(data.clin_ppg.v)-1]./data.clin_ppg.fs;

end

function make_plot(data)

% Plot settings
figure('Position', [20,20,600,300])
ftsize = 16; lwidth = 2;

% Finger PPG
plot(data.lab_ppg.t,data.lab_ppg.v, 'b', 'LineWidth', lwidth)
hold on
box off
xlabel('Time (s)', 'FontSize', ftsize)
ylabel('PPG (au)', 'FontSize', ftsize) % au - arbitrary units

% Ear PPG
plot(data.clin_ppg.t,data.clin_ppg.v, 'r', 'LineWidth', lwidth)
box off

% Tidy up plot
set(gca,'FontSize',ftsize,'YTick',[])
legend({'Laboratory Equipment', 'Clinical Device'},'Location','NorthEast')
ylim([0,1])

end

function save_plot(up)

save_path = [up.savepath, 'clinical_vs_lab_ppg'];
%print(gcf, '-depsc', save_path)
print(gcf, '-dsvg', save_path)
print(gcf, '-dpng', save_path)
fid = fopen([save_path, '.txt'], 'w');
fprintf(fid, ['Created using ' mfilename, ', ', date]);
fclose(fid);
close all;

fprintf('\n\n Finished: Files saved at: %s', up.savepath);

end
